<?php

namespace app\admin\controller;

use app\BaseController;
use think\facade\Db;
use think\facade\View;


class Base extends BaseController
{
    public function initialize()
    {
        // echo "父类控制器";
        $loginAdmin = session('adminSessionData');
        if ($loginAdmin){
            View::assign('loginUser',$loginAdmin['username']);
            $this->adminAuth($loginAdmin);
        }
    }

    /**
     * 删除
     */
    public function del()
    {
        // halt($_SERVER);
        $id = input('id');
        $dbname = input('dbname');
        $res = Db::name($dbname)->delete($id);
        if ($res) {
            return alert('操作成功', $_SERVER['HTTP_REFERER'], 6);
        } else {
            return alert('操作失败', $_SERVER['HTTP_REFERER'], 5);
        }
    }
    /**
     * 修改状态
     */
    public function status()
    {
        // halt($_SERVER);
        $id = input('id');
        $status = input('status');
        $dbname = input('dbname');
        $res = Db::name($dbname)->where('id',$id)->update(['status' => $status]);
        if ($res) {
            return alert('操作成功', $_SERVER['HTTP_REFERER'], 6);
        } else {
            return alert('操作失败', $_SERVER['HTTP_REFERER'], 5);
        }
    }

    /**
     * 密码加密
     */
    public function password_salt($str){
        return md5($this->salt.$str);
    }

    public function adminAuth($loginAdmin)
    {
        // halt(request());
        $currentRule = request()->controller(). '/' . request()->action();
        $rules = Db::name("auth_group")->where('id',$loginAdmin['group_id'])->value('rules');
        // halt($rules);
        $rulesArr = explode(',',$rules);
        foreach ($rulesArr as $k=>$v){
            $authRuleData = Db::name("auth_rule")->find($v);
            // echo $authRuleData['name']."----". $currentRule;
            if ($authRuleData['name'] == $currentRule){
                return true;
            }
        }
        echo "没有权限";
        // halt($rulesArr);

    }
}
